﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using JavaSubstitutes;

namespace Sprites
{
	public class TitleScene : Scene
	{
		private MarioComponent component;
		private int tickCount;
		private BgRenderer bgLayer0;
		private BgRenderer bgLayer1;

		public TitleScene(MarioComponent component, GraphicsConfiguration gc)
		{
			this.component = component;
			bgLayer0 = new BgRenderer(BgLevelGenerator.createLevel(2048, 15, false, LevelGenerator.TYPE_OVERGROUND), gc, 320, 240, 1);
			bgLayer1 = new BgRenderer(BgLevelGenerator.createLevel(2048, 15, true, LevelGenerator.TYPE_OVERGROUND), gc, 320, 240, 2);
		}

		public override void init()
		{
			Art.startMusic(4);
		}

		public override void render(WriteableBitmap wbmp, double alpha)
		{
			bgLayer0.setCam(tickCount + 160, 0);
			bgLayer1.setCam(tickCount + 160, 0);			
			bgLayer1.render(wbmp, tickCount, alpha);
			bgLayer0.render(wbmp, tickCount, alpha);

			int yo = 16 - Math.Abs((int)(Math.Sin((tickCount + alpha) / 6.0) * 8));
			if (Art.logo != null) wbmp.drawImage(Art.logo, 0, yo);
			if (Art.titleScreen != null) wbmp.drawImage(Art.titleScreen, 0, 120);
		}

		private void drawString(Graphics g, String text, int x, int y, int c)
		{
			char[] ch = text.toCharArray();
			for (int i = 0; i < ch.Length; i++)
			{
				g.drawImage(Art.font[ch[i] - 32, c], x + i * 8, y, null);
			}
		}


		private bool wasDown = true;
		public override void tick()
		{
			tickCount++;
			if (!wasDown && keys[Mario.KEY_JUMP])
			{
				component.ShowStartGameScene();
			}
			if (keys[Mario.KEY_JUMP])
			{
				wasDown = false;
			}
		}

		public double getX(double alpha)
		{
			return 0;
		}

		public double getY(double alpha)
		{
			return 0;
		}

	}
}